/** @file               sw_time.h
 *  @copyright          Copyright (C) 2006-2026 flyfish (tenggui_fu@126.com)
 *  @brief              This file is part of the swallow library
 *  @author             $Author: flyfish $
 *  @version            $Rev: 385 $
 *  @date               $Date: 2016-12-22 00:12:05 +0800 (周四, 22 12月 2016) $
 */
#ifndef __SW_TIME_H__
#define __SW_TIME_H__

#ifndef WIN32
#include <sys/time.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif

typedef struct _SW_DATETIME
{
    SW_UINT16 year;      /**< The number of years, larger than 1900 */
    SW_UINT16 mon;       /**< The number of months since January, in the range 1 to 12 */
    SW_UINT16 yday;      /**< The number of days since January 1, in the range 0 to 365 */
    SW_UINT16 mday;      /**< The day of the month, in the range 1 to 31 */
    SW_UINT16 wday;      /**< The number of days since Sunday, in the range 0 to 6 */
    SW_UINT16 hour;      /**< The number of hours past midnight, in the range 0 to 23 */
    SW_UINT16 min;       /**< The number of minutes after the hour, in the range 0 to 59 */
    SW_UINT16 sec;       /**< The number of seconds after the minute, normally in the range 0 to 59, but can be up to 60 to allow for leap seconds */
}SW_DATETIME;

typedef struct _SW_DATE
{
    SW_UINT16 year;      /**< The number of years, larger than 1900 */
    SW_UINT16 mon;       /**< The number of months since January, in the range 1 to 12 */
    SW_UINT16 yday;      /**< The number of days since January 1, in the range 0 to 365 */
    SW_UINT16 mday;      /**< The day of the month, in the range 1 to 31 */
    SW_UINT16 wday;      /**< The number of days since Sunday, in the range 0 to 6 */
}SW_DATE;

typedef struct _SW_TIME
{
    SW_UINT16 hour;      /**< The number of hours past midnight, in the range 0 to 23 */
    SW_UINT16 min;       /**< The number of minutes after the hour, in the range 0 to 59 */
    SW_UINT16 sec;       /**< The number of seconds after the minute, normally in the range 0 to 59, but can be up to 60 to allow for leap seconds */
}SW_TIME;

#if defined(WIN32) && !defined(__MINGW32__) && !defined(SW_V_WITH_PTHREAD)
typedef struct _SW_TIMESPEC
{
    time_t tv_sec;
    long tv_nsec;
}SW_TIMESPEC;
typedef struct timeval SW_TIMEVAL;
#else
typedef struct timespec SW_TIMESPEC;
typedef struct timeval SW_TIMEVAL;
#endif

SW_API SW_RET SW_InitTimeAPI(SW_VOID);
SW_API SW_RET SW_RunTimeAPI(SW_VOID);
SW_API SW_RET SW_StopTimeAPI(SW_VOID);
SW_API SW_RET SW_ExitTimeAPI(SW_VOID);
SW_API const SW_CHAR *SW_GetWdayAbbrStr(SW_UINT16 wday);
SW_API const SW_CHAR *SW_GetWdayFullStr(SW_UINT16 wday);
SW_API SW_UINT16 SW_GetWday(const SW_CHAR *pStr);
SW_API SW_UINT64 SW_GetCurElapse(SW_VOID);
SW_API SW_UINT64 SW_DateTimeToElapse(const SW_DATETIME *pDateTime);
SW_API SW_UINT64 SW_GetCurElapseMSec(SW_VOID);
SW_API SW_UINT64 SW_GetCurElapseUSec(SW_VOID);
SW_API SW_RET SW_ElapseToDateTime(SW_DATETIME *pDateTime, SW_UINT64 elapse);
SW_API SW_RET SW_ElapseToDateTimeStr(SW_CHAR *pStr, SW_UINT32 strLen, const SW_CHAR *pFormat, SW_UINT64 elapse);
SW_API SW_RET SW_GetCurDateTime(SW_DATETIME *pDateTime);
SW_API SW_RET SW_GetAbsTimeSpec(SW_UINT32 msec, SW_TIMESPEC *pTimeSpec);
SW_API SW_RET SW_GetTimeVal(SW_UINT32 msec, SW_TIMEVAL *pTimeVal);
SW_API SW_RET SW_GetDateTimeStr(SW_CHAR *pStr, SW_UINT32 strLen, const SW_CHAR *pFormat, const SW_DATETIME *pDateTime);
SW_API SW_RET SW_GetCurDateTimeStr(SW_CHAR *pStr, SW_UINT32 strLen, const SW_CHAR *pFormat);

#ifdef __cplusplus
}
#endif

#endif
